package com.estar.integaration.Parser;

import com.estar.integaration.Util.DateParseUtil;
import com.estar.integaration.Util.Util;
import com.estar.integaration.entity.paymentPlanEntity.PurchaseOrderDetail;
import io.swagger.models.auth.In;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Author: YJL
 * Date: 2022/3/12 22:55
 **/
public class NaturalSentenceParse {


  public List<PurchaseOrderDetail> parsePurchaseOrderDetailListFromNaturalSentence(String naturalSentence,List<PurchaseOrderDetail> purchaseOrderDetailList){
    List<PurchaseOrderDetail> newPurchaseOrderDetailList = new ArrayList<>();//经过解析和增加之后
    for(int i = 0; i < purchaseOrderDetailList.size();i++){
      PurchaseOrderDetail purchaseOrderDetail = purchaseOrderDetailList.get(i);


    }
    return purchaseOrderDetailList;
  }

  /**
   * 根据自然语言对其进行操作
   *   /**
   *    * 自然语言类型
   *    * 0 例如:60%*总金额
   *    * 1 例如: 如果入库单号为空，则账期为0天+180天+365天。0天：40%*总金额，180天之后：30%*总金额,365天后：10%*总金额； 如果入库单号有，180天之后：30%*总金额,365天后：10%*总金额
   *    * {[如果入库单号为空，则账期为0天+90天。][(0天：70%*总金额)，(180天之后：10%*总金额；)]} {如果入库单号有，则90天之后：10%*入库金额 }
   */
  public List<PurchaseOrderDetail> operateThisPODfromNaturalSentence(List<PurchaseOrderDetail> purchaseOrderDetailList,PurchaseOrderDetail purchaseOrderDetail,String sentence){
      List<PurchaseOrderDetail> newPurchaseOrderDetailList = new ArrayList<>();
      Integer type = getSentenceType(sentence);
      if(type == 0){
        List<Integer> lessSummaryNumbers = getIntegersFromSentence(sentence);
        for(PurchaseOrderDetail pod : purchaseOrderDetailList){
          pod.setNeedPayAmount(pod.getNeedPayAmount()*lessSummaryNumbers.get(0)*0.01);
          newPurchaseOrderDetailList.add(pod);
        }
      }else if(type == 1){
        String[] frontChildSentence = sentence.split("。");//如果入库单号为空，则账期为0天+180天+365天 | 0天：40%*总金额，180天之后：30%*总金额,365天后：10%*总金额； 如果入库单号有，180天之后：30%*总金额,365天后：10%*总金额
        String[] backChildSentence = frontChildSentence[1].split("；"); // 0天：40%*总金额，180天之后：30%*总金额,365天后 10%*总金额 | 如果入库单号有，180天之后：30%*总金额,365天后：10%*总金额
//        List<Integer> summaryNumbers = getIntegersFromSentence(frontChildSentence[0]);//0,180,365
        List<Integer> numsNoInventoryDetail = getIntegersFromSentence(backChildSentence[0]);//0,40,180,30,365,10
        List<Integer> numsInventoryDetail = getIntegersFromSentence(backChildSentence[1]);//180,30,365,10
        //若无入库单号
        if(purchaseOrderDetail.getInventoryVoucherNumber() == null
            || purchaseOrderDetail.getInventoryVoucherNumber().equals("")
            || purchaseOrderDetail.getInventoryVoucherNumber().equals(" ")
            || purchaseOrderDetail.getInventoryVoucherNumber().equals("null")
        ){
          for (int i = 0; i < numsNoInventoryDetail.size() / 2 ; i++){
            Integer addDay = numsNoInventoryDetail.get(i*2);
            Integer moneyPercentage = numsNoInventoryDetail.get(i*2 + 1);
            List<PurchaseOrderDetail> tmpPurchaseOrderDetailList = setAllDateandMoneyinMaterialFromList(purchaseOrderDetailList,addDay,moneyPercentage);
//            PurchaseOrderDetail tmpPurchaseOrderDetail =clonePurchaseOrderDetail(purchaseOrderDetail);
//            //ExpectPayDate是解析出结果之后的日期
//            tmpPurchaseOrderDetail.setExpectPayDate(DateParseUtil.dateAddAccountPeriod(tmpPurchaseOrderDetail.getExpectPayDate(),String.valueOf(addDay)));
//            //原本的钱拆散
//            tmpPurchaseOrderDetail.setNeedPayAmount(tmpPurchaseOrderDetail.getNeedPayAmount()*moneyPercentage*0.01);
            newPurchaseOrderDetailList.addAll(tmpPurchaseOrderDetailList);
          }
        }else {//若有入库单号
          for (int i = 0; i < numsInventoryDetail.size() / 2 ; i++){
            Integer addDay = numsInventoryDetail.get(i*2);
            Integer moneyPercentage = numsInventoryDetail.get(i*2 + 1);
            List<PurchaseOrderDetail> tmpPurchaseOrderDetailList = setAllDateandMoneyinMaterialFromList(purchaseOrderDetailList,addDay,moneyPercentage);
//            PurchaseOrderDetail tmpPurchaseOrderDetail = clonePurchaseOrderDetail(purchaseOrderDetail);
//            //原本的时间加上需要加上的时间
//            tmpPurchaseOrderDetail.setExpectPayDate(DateParseUtil.dateAddAccountPeriod(tmpPurchaseOrderDetail.getExpectPayDate(),String.valueOf(addDay)));
//            //原本的钱拆散
//            tmpPurchaseOrderDetail.setNeedPayAmount(tmpPurchaseOrderDetail.getNeedPayAmount()*moneyPercentage*0.01);
            newPurchaseOrderDetailList.addAll(tmpPurchaseOrderDetailList);
          }
        }
      }
      return newPurchaseOrderDetailList;
  }

  /**
   * 每个物料都要拆成这样的n个
   * 如果是两个交期，则每个物料进行拆分成二
   * @param purchaseOrderDetailList
   * @return
   */
  private List<PurchaseOrderDetail> setAllDateandMoneyinMaterialFromList(List<PurchaseOrderDetail> purchaseOrderDetailList,Integer addDay,Integer moneyPercentage){
    List<PurchaseOrderDetail> newPurchaseOrderDetailList = new ArrayList<>();
    //整个list进行，本次拆开的更新循环，例如这是第0天，百分之30,所有物料均加0乘百分之三十
    for(PurchaseOrderDetail p : purchaseOrderDetailList){
      //原pod->新pod
      PurchaseOrderDetail tmpPurchaseOrderDetail = clonePurchaseOrderDetail(p);
      //ExpectPayDate是解析出结果之后的加上拆开日期的最终日期
      tmpPurchaseOrderDetail.setExpectPayDate(DateParseUtil.dateAddAccountPeriod(tmpPurchaseOrderDetail.getExpectPayDate(),String.valueOf(addDay)));
      //原本的钱拆散
      tmpPurchaseOrderDetail.setNeedPayAmount(tmpPurchaseOrderDetail.getNeedPayAmount()*moneyPercentage*0.01);
      newPurchaseOrderDetailList.add(tmpPurchaseOrderDetail);
    }
    return newPurchaseOrderDetailList;
  }

  /**
   * 克隆老物料到新物料
   * @param purchaseOrderDetail
   * @return
   */
  public PurchaseOrderDetail clonePurchaseOrderDetail(PurchaseOrderDetail purchaseOrderDetail){
    PurchaseOrderDetail newPurchaseOrderDetail = new PurchaseOrderDetail(Util.getUUID(),
        purchaseOrderDetail.getOrderNumber(),
        purchaseOrderDetail.getPurchaseGroup(),
        purchaseOrderDetail.getSupplierName(),
        purchaseOrderDetail.getMaterialNumber(),
        purchaseOrderDetail.getMaterialName(),
        purchaseOrderDetail.getSpec(),
        purchaseOrderDetail.getDeliveryGoodsDate(),
        purchaseOrderDetail.getPredeterminedNumber(),
        purchaseOrderDetail.getPriceWithTax(),
        purchaseOrderDetail.getPriceTaxGather(),
        purchaseOrderDetail.getInventoryDate(),
        purchaseOrderDetail.getInventoryVoucherNumber(),
        purchaseOrderDetail.getInventoryMoney(),
        purchaseOrderDetail.getDueNumber(),
        purchaseOrderDetail.getAmountDue(),
        purchaseOrderDetail.getPayAmount(),
        purchaseOrderDetail.getDrawingNo(),
        purchaseOrderDetail.getBrand(),
        purchaseOrderDetail.getExpectPayDate(),
        purchaseOrderDetail.getNeedPayAmount(),
        purchaseOrderDetail.getType(),
        purchaseOrderDetail.getIfSpecial(),
        purchaseOrderDetail.getTicketComeTime(),
        purchaseOrderDetail.getTaxPoint(),
        purchaseOrderDetail.getNeedPayDate()
    );
    return newPurchaseOrderDetail;
  }





  private List<Integer> getIntegersFromSentence(String sentence){
    List<Integer> numbers = new LinkedList<Integer>();
    String regEx = "\\d+";
    Pattern p = Pattern.compile(regEx);
    Matcher m = p.matcher(sentence);
    while (m.find()){
      numbers.add(Integer.parseInt(m.group()));
    }
    return numbers;
  }

  /**
   * 自然语言类型
   * 0 例如:60%*总金额
   * 1 例如: 如果入库单号为空，则账期为0天+180天+365天。0天：40%*总金额，180天之后：30%*总金额,365天后：10%*总金额； 如果入库单号有，180天之后：30%*总金额,365天后：10%*总金额
   * {如果入库单号为空，则账期为0天+90天。0天：70%*总金额，180天之后：10%*总金额；} { 如果入库单号有，则90天之后：10%*入库金额 }
   *
   */
  private Integer getSentenceType(String sentence){
    if(sentence.length() < 10){
      return 0;
    }else {
      return 1;
    }
  }





}
